<template>
    <div class="login">
        <h2>登录</h2>
        <form @submit.prevent="handleSubmit">
            <div>
                <label for="username">用户名:</label>
                <input v-model="username" id="username" type="text" required />
            </div>
            <div>
                <label for="password">密码:</label>
                <input v-model="password" id="password" type="password" required />
            </div>
            <div>
                <button type="submit">登录</button>
            </div>
        </form>
        <p v-if="message">{{ message }}</p>
    </div>
</template>

<script>
import axios from 'axios';

export default {
    data() {
        return {
            username: '',
            password: '',
            message: '',
        };
    },
    methods: {
        async handleSubmit() {
            try {
                const response = await axios.post('/api/login', {
                    username: this.username,
                    password: this.password,
                });
                if (response.data.code === 200) {
                    this.message = '登录成功！';



                    localStorage.setItem('userInfo', JSON.stringify({
                        username: this.username,
                        //写了一些假的admin数据
                        gender: '男',
                        grade: '一年级',
                        phone: '12345678910',
                        email: '12345678910@qq.com',


                        firstName: 'Admin',
                        lastName: 'User',
                        password: '123456',
                        mobile: '12345678910',
                        email: '12345678910@qq.com',
                        role: 'CEO',
                        isAdmin: false,
                        company: 'UnKnowServe',
                        preferredBusinessName: 'UnKnowServe',
                        photo: null
                    }));


                    this.$router.push('/home');
                } else {
                    this.message = response.data.message;
                }
            } catch (error) {
                this.message = '登录失败，请重试。';
            }
        }
    }
};
</script>

<style scoped>
.login {
    width: 320px;
    margin: 100px auto;
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 10px;
    background-color: #fff;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    animation: fadeIn 0.5s ease;
}

h2 {
    text-align: center;
    color: #42b983;
    font-size: 24px;
}

form div {
    display: flex;
    align-items: center;
    margin-bottom: 15px;
}

label {
    width: 70px;
    margin-right: 10px;
    text-align: right;
    color: #333;
}

input,
select {
    flex: 1;
    padding: 10px;
    border-radius: 5px;
    border: 1px solid #ccc;
    font-size: 16px;
}

button {
    width: 100%;
    padding: 10px;
    background-color: #42b983;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    transition: background-color 0.3s ease;
}

button:hover {
    background-color: #358d73;
}

p {
    color: red;
    text-align: center;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}
</style>